// pages/evaluateevent/evaluateevent.js
const {
  $Message
} = require('../../iview/base/index');

var app = getApp();
const db = wx.cloud.database()
const user = db.collection("user")
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  /* 这一页需要传进来的参数是活动的id */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    var that = this
    const db = wx.cloud.database()
    const eventco = db.collection("event")
    eventco.where({
      _id: options.id
    }).get().then(function (res) {
      that.setData({
        event: res.data[0]
      })
    })

  },

  inputChange(e) {
    var that = this
    switch (e.currentTarget.id) {
      case "comment": {
        that.setData({
          comment: e.detail.value
        })
      }
      break
    case "score": {
      that.setData({
        score: e.detail.value
      })
    }
    break
    }
  },

  commit: function (e) {
    var that = this
    const db = wx.cloud.database()
    const eventco = db.collection("event")
    if (this.data.comment != undefined && this.data.score != undefined) {
      if (that.data.score > 5) {
        $Message({
          content: '活动评分不能超过5分',
          type: "default"
        })
      } else {

        user.where({
            _openid: app.globalData.openID
          }).get()
          .then(res => {
            let myevents = res.data[0].myevents
            let mynewevents = []
            myevents.forEach(item => {
              if (item.event_id != that.data.id) {
                mynewevents.push({
                  event_id: item.event_id,
                  score: item.score
                })              
              }            

            }                     
            )
            mynewevents.push({
              event_id: that.data.id,
              score: parseInt(that.data.score)
            })

            console.log('mynewevents为：'+JSON.stringify(mynewevents))
            that.setData({
              mynewevents:mynewevents
            })

            console.log('that data mynewevents'+JSON.stringify(that.data.mynewevents))
          /*  wx.cloud.callFunction({
              name: "eventFeedback",
              data: {
                mynewevents:that.data.mynewevents,
                openid:app.globalData.openID
                
              }
            })
            */

           var temp
           eventco.where({
             _id: that.data.id
           }).get().then(function (oldinfo) {
             temp = oldinfo.data[0].feedback
             if (temp == undefined) {
               temp = [{
                 participant: app.globalData.openID,
                 comment: that.data.comment,
                 score: parseInt(that.data.score)
               }]
             } else {
               temp.push({
                 participant: app.globalData.openID,
                 comment: that.data.comment,
                 score: parseInt(that.data.score)
               })
             }
           }).then(function (newinfo) {
             wx.cloud.callFunction({
               name: "eventFeedback",
               data: {
                 id: that.data.id,
                 feed: temp,
                 mynewevents:that.data.mynewevents,
                 openid:app.globalData.openID,
                 
               }
             }).then(function (e) {
               console.log(e)
               $Message({
                 content: '评价成功',
                 type: "success"
               })
               wx.navigateBack({
                 delta: 1,
               })
             }).catch(function (err) {
               console.log(err)
               $Message({
                 content: '评价失败，请联系开发者',
                 type: 'error'
               })
             })
           })


          })

     

      }
    } else {
      $Message({
        content: '请填写完整信息喔~',
        type: "defalut"
      });
    }
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})